package com.bd;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AlarmsModule {
       
    public final static String TABLE_NAME = "AlarmsModule";
    public final static String COL_PK = "id";
    public final static String COL_PET_ID = "petid";
    public final static String COL_ACTIVITY = "activity";
    public final static String COL_DAYS = "days";
    public final static String COL_HOUR = "hour";
    public final static String COL_MINUTE = "minute";
    public final static String COL_AMPM = "ampm";
    
    public final static String[] ALL_COLS =  {
    	AlarmsModule.COL_PK, 
    	AlarmsModule.COL_PET_ID, 
    	AlarmsModule.COL_ACTIVITY, 
    	AlarmsModule.COL_DAYS,
    	AlarmsModule.COL_HOUR,
    	AlarmsModule.COL_MINUTE,
    	AlarmsModule.COL_AMPM};
    
    

	private int pk = -1;
    private String petid = "";
    private String activity = "";
    private String days = "";
    private int hour = 0;
	private int minute = 0;
    private String ampm = "";     
       
    public AlarmsModule(String petid, String activity, String days, int hour, int minute, String ampm){
           
        this.petid = petid;
        this.activity = activity;
        this.days = days;
        this.hour = hour;
        this.minute = minute;
        this.ampm = ampm;
           
    }
       
    private AlarmsModule(){}
       
       

    @Override
    public String toString() {
        return "Alarm [pk=" + this.pk + ", petid=" + this.petid + ", activity=" + this.activity + ", days=" + this.days + 
        		", hour=" + this.hour+ ", minute=" + this.minute+ ", ampm=" + this.ampm + "]";
    }
       
    /***************************************************************************
     * SETTERS AND GETTERS
     **************************************************************************/
	public int getPk() {
        return pk;
    }

    public void setPk(int pk) {
        this.pk = pk;
    }

    public String getPetid() {
		return petid;
	}

	public void setPetid(String petid) {
		this.petid = petid;
	}

	public String getActivity() {
		return activity;
	}

	public void setActivity(String activity) {
		this.activity = activity;
	}

	public String getDays() {
		return days;
	}

	public void setDays(String days) {
		this.days = days;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public String getAmpm() {
		return ampm;
	}

	public void setAmpm(String ampm) {
		this.ampm = ampm;
	}

    /***************************************************************************
     * createTableString
     **************************************************************************/
    public static String createTableString(){
        String create = "CREATE TABLE " + AlarmsModule.TABLE_NAME + "(";
       
        create += AlarmsModule.COL_PK + " INTEGER PRIMARY KEY, ";
        create += AlarmsModule.COL_PET_ID + " STRING, ";
        create += AlarmsModule.COL_ACTIVITY + " INTEGER, ";
        create += AlarmsModule.COL_DAYS + " STRING, ";
        create += AlarmsModule.COL_HOUR + " STRING, ";
        create += AlarmsModule.COL_MINUTE + " STRING, ";
        create += AlarmsModule.COL_AMPM + " STRING";
   
        create += ");";
       
        return create;
    }
   
   
    /***************************************************************************
     * insertModule
     **************************************************************************/
    public AlarmsModule insertAlarm(SQLiteDatabase database){
       
        ContentValues values = new ContentValues();
        values.put(AlarmsModule.COL_PET_ID,this.getPetid());
	    values.put(AlarmsModule.COL_ACTIVITY,this.getActivity());
	    values.put(AlarmsModule.COL_DAYS,this.getDays());
	    values.put(AlarmsModule.COL_HOUR,this.getHour());
	    values.put(AlarmsModule.COL_MINUTE,this.getMinute());
	    values.put(AlarmsModule.COL_AMPM,this.getAmpm());
	   
	    long insertId = database.insert(AlarmsModule.TABLE_NAME, null, values);
	   
	    return AlarmsModule.getAlarm(database, insertId);
    }
       
    /***************************************************************************
     * updateModule
     **************************************************************************/
    public boolean updateAlarm(SQLiteDatabase database){
       
        ContentValues values = new ContentValues();
        values.put(AlarmsModule.COL_PET_ID,this.getPetid());
	    values.put(AlarmsModule.COL_ACTIVITY,this.getActivity());
	    values.put(AlarmsModule.COL_DAYS,this.getDays());
	    values.put(AlarmsModule.COL_HOUR,this.getHour());
	    values.put(AlarmsModule.COL_MINUTE,this.getMinute());
	    values.put(AlarmsModule.COL_AMPM,this.getAmpm());
       
        String whereClause = AlarmsModule.COL_PK + "= \"" + this.pk + "\"";
       
        int affected = database.update(AlarmsModule.TABLE_NAME, values,
                whereClause, null);
       
        if(affected==0){
            return false;
        }
        return true;
    }
       
    /***************************************************************************
     * getModule
     **************************************************************************/
    public static AlarmsModule getAlarm(SQLiteDatabase database, long insertId) {
       
        String selection = AlarmsModule.COL_PK + "=" + insertId;
       
        Cursor cursor = database.query(AlarmsModule.TABLE_NAME, AlarmsModule.ALL_COLS,
                        selection, null, null, null, null);
       
        cursor.moveToFirst();
        AlarmsModule module = AlarmsModule.cursorToPet(cursor);
        cursor.close();
        return module;
    }
       
    public static AlarmsModule getAlarm(SQLiteDatabase database, String id) {
       
        String selection = AlarmsModule.COL_PK + " = \"" + id + "\"";
       
        Cursor cursor = database.query(AlarmsModule.TABLE_NAME, AlarmsModule.ALL_COLS,
                selection, null, null, null, null);
       
        cursor.moveToFirst();
        AlarmsModule module = AlarmsModule.cursorToPet(cursor);
        cursor.close();
        return module;
    }
    /***************************************************************************
     * deleteModule
     **************************************************************************/
    public void deleteAlarm(SQLiteDatabase database){
       
        //delete from the Module table
        database.delete(AlarmsModule.TABLE_NAME, AlarmsModule.COL_PK + "=" + this.pk, null);
        System.out.println("Alarm deleted: " + this.toString());
    }
       
    /***************************************************************************
     * cursorToModule
     **************************************************************************/
    private static AlarmsModule cursorToPet(Cursor cursor) {
       
        AlarmsModule Module = new AlarmsModule();
        Module.setPk(cursor.getInt(0));
        Module.setPetid(cursor.getString(1));
        Module.setActivity(cursor.getString(2));
        Module.setDays(cursor.getString(3));
        Module.setHour(cursor.getInt(4));
        Module.setMinute(cursor.getInt(5));
        Module.setAmpm(cursor.getString(6));
        return Module;
    }
       
        /***************************************************************************
     * getAllModules
     **************************************************************************/
	public static List<AlarmsModule> getAllAlarms(SQLiteDatabase database) {
        List<AlarmsModule> modules = new ArrayList<AlarmsModule>();
       
        Cursor cursor = database.query(AlarmsModule.TABLE_NAME, AlarmsModule.ALL_COLS, null,
                null, null, null, null);
       
        if(cursor.getCount()==0){
            cursor.close();
            return modules;
        }
       
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            AlarmsModule module = AlarmsModule.cursorToPet(cursor);
            modules.add(module);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return modules;
    }
	
	public static List<AlarmsModule> getAllAlarmsFromPet(SQLiteDatabase database, String petid) {
        List<AlarmsModule> modules = new ArrayList<AlarmsModule>();
       
        String selection = AlarmsModule.COL_PET_ID + "=" + petid;
        
        Cursor cursor = database.query(AlarmsModule.TABLE_NAME, AlarmsModule.ALL_COLS,
                        selection, null, null, null, null);
       
        if(cursor.getCount()==0){
            cursor.close();
            return modules;
        }
       
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            AlarmsModule module = AlarmsModule.cursorToPet(cursor);
            modules.add(module);
            cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return modules;
    }
	
	
}
